//
// Created by Lenovo on 2020/10/13.
//

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long int
#define MAX 100000+10
using namespace std;
long long a[MAX],r[MAX];
long long exgcd(ll a,ll b,ll &x,ll &y) //扩展欧几里得
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    ll res=exgcd(b,a%b,y,x);
    y=y-x*(a/b);
    return res;
}
long long solve(ll a[],ll b[],ll n)
{
    for(int i=1;i<n;i++)
    {
        ll x,y;
        ll c=r[i]-r[i-1];
        ll gcd=exgcd(a[i-1],a[i],x,y);
        if(c%gcd!=0)
            return -1;
        else
        {
            ll t=a[i]/gcd;
            x=(x*(c/gcd)%t+t)%t;
            r[i]=a[i-1]*x+r[i-1];
            a[i]=a[i-1]*(a[i]/gcd);
        }
    }
    return r[n-1];
}
int main()
{
    ll n;
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
        {
            cin>>a[i]>>r[i];
        }
        long long ans=solve(a,r,n);
        cout<<ans<<endl;
    }
    return 0;
}